package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.DishFlavorMapper;
import cn.usian.dao.DishMapper;
import cn.usian.entity.DTO;
import cn.usian.entity.Dish;
import cn.usian.entity.DishFlavor;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public R<Page> page(int page, int pageSize, String name) {
        Page<DTO> page1 = new Page<>(page, 10);
        Page<DTO> dishPage = dishMapper.findPage(page1, name);
        return R.success(dishPage);
    }

    @Override
    public void del(long[] ids) {
        for (long id : ids) {
            dishMapper.deleteById(id);
        }
    }

    @Override
    public R status(Integer status, long[] ids) {
        Dish dish = new Dish();
        for (long id : ids) {
            dish.setId(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
        return R.success("修改成功");
    }

    @Override
    public void add(Dish dish, HttpSession session) {
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        Long id = (Long) session.getAttribute("employee");
        dish.setCreateUser(id);
        dish.setUpdateUser(id);
        dishMapper.insert(dish);
    }

    @Override
    public Long selectByName(String name) {
        return dishMapper.selectByName(name);
    }

    @Override
    public R findById(long id) {
        //List<DishFlavor> flavors = dishFlavorMapper.findByName(id);
        //先根据ID查询dish
        Dish dish = dishMapper.selectById(id);
        //先根据dish_id查询对应的口味
        List<DishFlavor> list = dishFlavorMapper.findByDishId(id);
        //返回的对象dto
        DTO dto = new DTO();
        //将dish对象中的数据复制到dto对象中
        BeanUtils.copyProperties(dish,dto);
        dto.setFlavors(list);
        return R.success(dto);
    }

    @Override
    public List<Dish> list(Long categoryId, String name) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        queryWrapper.like(name!=null,Dish::getName,name);
        return dishMapper.selectList(queryWrapper);
    }



   /* @Override
    public void add(Dish dish, HttpSession session) {
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        Long id = (Long) session.getAttribute("employee");
        dish.setCreateUser(id);
        dish.setUpdateUser(id);
        dishMapper.insert(dish);
    }*/


}
